{% template "admin/header.html" %}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>友情链接</legend>
    </fieldset>
    <blockquote class="layui-elem-quote">
        <a href="/admin/link/addUI" class="layui-btn layui-btn-sm">添加</a>
    </blockquote>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>

{% template "admin/footer.html" %}
<script type="text/html" id="url">
    <a href="{{d.Url}}" target="_blank">{{d.Url}}</a>
</script>
<script type="text/html" id="order">
    <input name="{{d.Link_id}}" data-id="{{d.Link_id}}" class="list_order layui-input" value="{{d.Sort}}" size="10"/>
</script>
<script type="text/html" id="open">
    <input type="checkbox" name="open" value="{{d.Link_id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="open" {{ isopen(d.Open) }}>
</script>
<script type="text/html" id="action">
    <a href="/admin/link/editUI?link_id={{d.Link_id}}" class="layui-btn layui-btn-xs">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>

    function isopen(o){
        return o==1?"checked":"";
    }

    layui.use(['table','form'], function() {
        var table = layui.table,form = layui.form, $ = layui.jquery;
        var tableIn = table.render({
            id: 'link',
            elem: '#list',
            url: '/admin/link/query',
            method: 'post',
            where:{},
            cols: [[
                /*{checkbox: true, fixed: true},*/
                {field: 'Link_id', title: 'ID', width: 80,sort: true},
                {field: 'Name', title: '链接名称', width: 200},
                {field: 'Url', title: '链接URL', width: 300,templet:'#url'},
                {field: 'Qq', title: 'QQ', width: 120},
                {field: 'Addtime', title: '时间', width: 150,sort: true},
                {field: 'Sort',align: 'center',title: '排序', width: 120, templet: '#order', sort: true},
                {field: 'Open',align: 'center', title: '状态', width: 100, sort: true,toolbar: '#open'},
                {width: 160, align: 'center', toolbar: '#action'}
            ]]
        });
        form.on('switch(open)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var open = obj.elem.checked===true?1:0;
            $.post('/admin/link/edit',{'link_id':id,'open':open},function (res) {
                layer.close(loading);
                if (res.Code==1) {
                    layer.msg(res.Msg,{icon:6});
                }else{
                    layer.msg(res.Msg,{time:1000,icon:2});
                    return false;
                }
            })
        });
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('您确定要删除该链接吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("/admin/link/delete",{link_id:data.Link_id},function(res){
                        layer.close(loading);
                        if(res.Code===1){
                            layer.msg(res.Msg,{time:1000,icon:1});
                            obj.del();
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });
        $('body').on('blur','.list_order',function() {
            var link_id = $(this).attr('data-id');
            var sort = $(this).val();
            $.post('/admin/link/edit',{link_id:link_id,sort:sort},function(res){
                if(res.Code === 1){
                    layer.msg(res.Msg, {time: 1000, icon: 1});
                }else{
                    layer.msg(res.Msg,{time:1000,icon:2});
                }
            })
        });

    })
</script>
</body>
</html>
